<?php

App::uses('Crud', 'Model');
App::uses('WorkstationConfig', 'Model');
App::uses('CertificationDetail', 'Model');
App::uses('Certification', 'Model');

class Link extends Crud
{
    /** Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

    /**
     * Obtenemos los dependencias por configuracion de la Operación
     * @param string $workstationConfigId UUID
     * @return array
     */
    public function getBySource($workstationConfigId) {
        $filters = array(
            'conditions' => array(
                'source' => $workstationConfigId,
                'status' => self::STATUS_ENABLED
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }

    /**
     * Buscamos por destino
     * @param string $destination UUID
     * @return array
     */
    public function getByDestination($destination) {
        $filters = array(
            'conditions' => array(
                'destination' => $destination,
                'status' => self::STATUS_ENABLED
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }

    public function getPropagation($userId, $source) {
        $filters = array(
            'fields' => array(
                'AVG(IFNULL(cd.qualification,0)) as qualification',
                'MIN(cd.certification_date) as certification_date',
            ),
            'joins' => array(
                array(
                    'table' => 'workstations_configs',
                    'alias' => 'wc',
                    'type' => 'INNER',
                    'conditions' => array(
                        'wc.id = Link.destination',
                        'wc.status' => WorkstationConfig::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'INNER',
                    'conditions' => array(
                        'w.id = wc.workstation_id',
                        'w.status' => Workstation::STATUS_ENABLED,
                    ),
                ),
                array(
                    'table' => 'certifications',
                    'alias' => 'c',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'w.id = c.workstation_id',
                        'c.status' => Certification::STATUS_ENABLED,
                        'c.user_id' => $userId,
                    ),
                ),
                array(
                    'table' => 'certifications_details',
                    'alias' => 'cd',
                    'type' => 'LEFT',
                    'conditions' => array(
                        'cd.certification_id = c.id',
                        'cd.status' => CertificationDetail::STATUS_ENABLED,
                        'cd.workstation_config_id = Link.destination',
                    ),
                ),
            ),
            'conditions' => array(
                'Link.source' => $source,
                'Link.status' => self::STATUS_ENABLED,
            ),
        );
        $record = $this->find('first', $filters);
        return $record;
    }

    public function getByLine($lineId) {
        $filters = array(
            'fields' => array(
                'Link.source as id',
                'l.name as line',
                'w.description as workstation',
                'wc.name as workstationConfig',
            ),
            'joins' => array(
                array(
                    'table' => 'workstations_configs',
                    'alias' => 'wc',
                    'type' => 'INNER',
                    'conditions' => array(
                        'wc.id = Link.destination',
                    ),
                ),
                array(
                    'table' => 'workstations',
                    'alias' => 'w',
                    'type' => 'INNER',
                    'conditions' => array(
                        'w.id = wc.workstation_id',
                    ),
                ),
                array(
                    'table' => 'lines',
                    'alias' => 'l',
                    'type' => 'INNER',
                    'conditions' => array(
                        'l.id = w.line_id',
                    ),
                )
            ),
            'conditions' => array(
                'Link.status' => self::STATUS_ENABLED,
                'l.id' => $lineId,
            ),
        );
        return $this->flatArray($this->find('all', $filters));
    }

}
